package com.mjh.zhujianju.service.agency.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.mjh.zhujianju.util.FileUrlAdapter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.livew.framework.base.model.FileEO;
import com.livew.framework.base.service.FileService;
import com.livew.framework.base.service.impl.BaseServiceImpl;
import com.livew.framework.enums.EStatus;
import com.livew.framework.exception.XTBusinessException;
import com.livew.framework.page.PagedBeanUtil;
import com.livew.framework.page.PagedResult;
import com.livew.framework.utils.XTEntityUtils;
import com.livew.framework.utils.XTListUtils;
import com.mjh.zhujianju.dao.agency.WorkGuideMapper;
import com.mjh.zhujianju.enums.BusinessEnum;
import com.mjh.zhujianju.enums.WorkGuideEnum;
import com.mjh.zhujianju.model.agency.WorkGuideEO;
import com.mjh.zhujianju.model.base.ContentEO;
import com.mjh.zhujianju.service.agency.WorkGuideSercice;
import com.mjh.zhujianju.service.base.ContentService;
import com.mjh.zhujianju.service.base.HitsService;
import com.mjh.zhujianju.vo.agency.WorkGuideVO;

/**
 * @author liuyang
 * @Description: 办事指南
 * @date 2018/8/14 22:22
 */
@Service
public class WorkGuideSerciceImpl extends BaseServiceImpl<WorkGuideEO> implements WorkGuideSercice {

    @Autowired
    private WorkGuideMapper workGuideMapper;

    @Autowired
    private FileService fileService;

    @Autowired
    private ContentService contentService;

    @Autowired
    private HitsService hitsService;

    public WorkGuideSerciceImpl(WorkGuideMapper workGuideMapper) {
        super(workGuideMapper);
    }

    @Override
    public Map<String, List<WorkGuideVO>> queryWorkGuideList() {
        Map<String, List<WorkGuideVO>> map = Maps.newHashMap();
        List<WorkGuideEO> workGuideList = workGuideMapper.selectAll();
        if (CollectionUtils.isEmpty(workGuideList)){
            return map;
        }

        List<Long> ids = workGuideList.stream().map(workGuideEO -> workGuideEO.getId()).distinct().collect(Collectors.toList());

        //查询图片
        List<FileEO> files = fileService.getFileByIds(ids,WorkGuideEO.class.getName(),null);
        if (CollectionUtils.isEmpty(files)){
            return map;
        }

        //组装图片
        List<WorkGuideVO> list = Lists.newArrayList();
        workGuideList.stream().forEach(workGuideEO -> {
            WorkGuideVO workGuideVO = new WorkGuideVO();
            BeanUtils.copyProperties(workGuideEO,workGuideVO);
            files.stream().forEach(fileEO -> {
                if (workGuideEO.getId().equals(fileEO.getCaseId())){
                    workGuideVO.setFileAction(FileUrlAdapter.assemblyAction(fileEO.getId()));
                }
            });
            list.add(workGuideVO);
        });

        //组装map
        for (WorkGuideEnum workGuideEnum : WorkGuideEnum.values()){
            String type = workGuideEnum.getCode();
            List<WorkGuideVO> voList = list.stream().filter(workGuideVO -> workGuideVO.getType().equals(type)).collect(Collectors.toList());
            map.put(type,voList);
        }
        return map;
    }

    @Override
    public WorkGuideVO queryWorkGuideInfo(Long id) {
        WorkGuideVO workGuideVO = null;
        //详细信息
        WorkGuideEO workGuideEO = workGuideMapper.selectByPrimaryKey(id);
        if (workGuideEO == null){
            return null;
        }
        workGuideVO = new WorkGuideVO();
        BeanUtils.copyProperties(workGuideEO,workGuideVO);
        String type = BusinessEnum.TYPE_WORKGUIDE.getCode();

        //图文详情
        ContentEO contentEO = contentService.queryContentByTypeAndId(type,workGuideEO.getId());

        if (contentEO != null){
            workGuideVO.setContent(contentEO.getContent());
        }

        //浏览量+1
        //添加浏览量
        hitsService.updateNumber(type,id);

        //查询浏览量
        Long hitsTotal = hitsService.queryHitsTotal(type,id);
        workGuideVO.setTotalHits(hitsTotal);

        return workGuideVO;
    }
    
    
    
    @Override
  	public void saveInfo(WorkGuideVO vo) throws XTBusinessException {
  		WorkGuideEO eo = new WorkGuideEO();
  		BeanUtils.copyProperties(vo, eo);
  		eo.setStatus(EStatus.active.getCode());
//  		OrganizationEO org =	organizationService.get();
//  		if(org!=null) {
//  			eo.setDeptId(org.getId());
//  		}
  		this.save(eo);
  		if (!XTListUtils.isEmpty(vo.getFileIds())) {
  			fileService.updateList(vo.getFileIds(), eo.getId(), WorkGuideEO.class.getName());
  		}
  		if (vo.getContent() != null) {
  			contentService.saveOrUpdate(BusinessEnum.TYPE_WORKGUIDE.getCode(), eo.getId(), vo.getContent());
  		}
  		
  	}

  	@Override
  	public void updateInfo(WorkGuideVO vo) throws XTBusinessException {
  		WorkGuideEO eo = new WorkGuideEO();
  		BeanUtils.copyProperties(vo, eo);
  		eo.setStatus(EStatus.active.getCode());
  		this.updateByPrimaryKeySelective(eo);
  		if (!XTListUtils.isEmpty(vo.getFileIds())) {
  			fileService.updateList(vo.getFileIds(), eo.getId(), WorkGuideEO.class.getName());
  		}
  		// 更新内容
  		contentService.saveOrUpdate(BusinessEnum.TYPE_WORKGUIDE.getCode(), eo.getId(), vo.getContent());
  	}

  	@Override
  	public PagedResult<WorkGuideVO> getVOPage(WorkGuideVO vo) {
  		Integer pageNo = vo.getPageNo() == null ? 1 : vo.getPageNo();
  		Integer pageSize = vo.getPageSize() == null ? 15 : vo.getPageSize();
  		PageHelper.startPage(pageNo, pageSize);
  		List<WorkGuideEO> list = workGuideMapper.selectPagination(vo);
  		PagedResult<WorkGuideEO> page = PagedBeanUtil.toPagedResult(list);
  		PagedResult<WorkGuideVO> result = new PagedResult<>();
  		BeanUtils.copyProperties(page, result);
  		if (!XTListUtils.isEmpty(list)) {
  			List<Long> ids = XTEntityUtils.getIdList(list);
  			//工作内容
  			List<WorkGuideVO> voList = new ArrayList<WorkGuideVO>();
  			Map<Long, FileEO> files = fileService.getMapTopOne(ids, WorkGuideEO.class.getName(), null);
			for (WorkGuideEO workGuideEO : list) {
				WorkGuideVO workGuideVO =new WorkGuideVO();
				BeanUtils.copyProperties(workGuideEO, workGuideVO);
				if(files!=null) {
				FileEO file = files.get(workGuideVO.getId());
				if(file!=null) {
					workGuideVO.setFileId(file.getId());	
				}
			}	
				workGuideVO.setTypeName(WorkGuideEnum.getName(workGuideVO.getType()));
				voList.add(workGuideVO);
  			}
  			result.setDataList(voList);
  		}
  		return result;
  	}

  	@Override
  	public WorkGuideVO getInfo(Long id) throws XTBusinessException{
  		WorkGuideEO wd = super.getByPrimaryKey(id);
  		if (wd != null) {
  			WorkGuideVO vo = new WorkGuideVO();
  			BeanUtils.copyProperties(wd, vo);
  			ContentEO content =	this.contentService.queryContentByTypeAndId(BusinessEnum.TYPE_WORKGUIDE.getCode(), wd.getId());
  			List<FileEO> files = fileService.getFileById(id, WorkGuideEO.class.getName(), null);
  			if(!XTListUtils.isEmpty(files)) {
  				vo.setFiles(files);
  		  		vo.setFileIds(XTEntityUtils.getIdList(files));
  			}
  			if(content!=null) {
  				vo.setContent(content.getContent());
  			}
  			vo.setTypeName(WorkGuideEnum.getName(vo.getType()));
  			return vo;
  		}

  		return null;
  	}
      
     
}
